﻿using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Admin.业务界面
{
    public partial class 手动分配 : Form
    {
        List<StuGroup> all_group;//全部组
        List<Teacher> all_teacher;//全部老师
        List<Student> all_students;//全部学生
        List<TeaChoice> all_tdata;//全部老师选择数据
        List<StuChoice> all_gdata;//全部组选择数据
        List<StuGroup> t_groups;//指定老师选择的所有组

        public 手动分配()
        {
            InitializeComponent();
        }

        private void 手动分配_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Hide();
            //管理员主界面 f1 = new 管理员主界面();
            //f1.ShowDialog();
        }

        private void 手动分配_Load(object sender, EventArgs e)//显示所有：组、老师。准备所有：学生、组选择数据、老师选择数据。
        {
            BLL.B_Admin b_Group = new BLL.B_Admin();
            all_group = b_Group.GetList_S_T();

            BLL.B_Admin b_Teacher = new BLL.B_Admin();
            all_teacher = b_Teacher.GetList_T();

            BLL.B_Admin b_Student = new BLL.B_Admin();
            all_students = b_Student.GetList_S();

            BLL.B_Admin b_T = new BLL.B_Admin();
            all_tdata = b_T.GetList_T_C();

            BLL.B_Admin b_G = new BLL.B_Admin();
            all_gdata = b_G.GetList_S_C();

            //if(all_gdata.Count != all_group.Count)
            //    return;

            Groups_view.DataSource = all_group;
            Teachers_view.DataSource = all_teacher;
        }

        private void Groups_view_MouseClick_1(object sender, MouseEventArgs e)
        {
            if (Groups_view.CurrentCell == null) return;

            int idx = Groups_view.CurrentCell.RowIndex;
            Disp_Group_info(idx, all_group);
        }

        private void Teachers_view_MouseClick_1(object sender, MouseEventArgs e)
        {
            teach_Group.DataSource = null;
            Disp_teachers_group();
        }

        private void Disp_teachers_group()
        {
            if (Teachers_view.CurrentCell == null) return;

            int idx = Teachers_view.CurrentCell.RowIndex;

            var t_select = all_tdata.Where(ts => ts.Teacher_id == all_teacher[idx].teacher_id).ToList();
            if (t_select.Count == 0) return;

            List<int> g_ids = new List<int>();

            for (int i = 1; i <= 5; i++)
            {
                var g = get_Choice(t_select[0], $"Assign_{i}");
                if (g != -1) g_ids.Add(g);
            }
            
            t_groups = all_group.Where(g => g_ids.Contains(g.group_id)).ToList();
            teach_Group.DataSource = t_groups;
        }

        private int get_Choice(object Data, string pro_name)
        {
            var tChoice = Data.GetType().GetProperty(pro_name).GetValue(Data, null);
            return Object.ReferenceEquals(tChoice, null) ? -1 : (int)tChoice;
        }

        private void set_Choice(object Data, string pro_name, int g_id)
        {
            Data.GetType().GetProperty(pro_name).SetValue(Data, g_id);
        }

        //修改老师、学生组配对
        private void modify_pair_Click_1(object sender, EventArgs e)//手动分配按钮
        {
            if (Groups_view.CurrentCell == null) return;

            int dgview_idx = Groups_view.CurrentCell.RowIndex;
            var g_id = all_group[dgview_idx].group_id; //网格中选中的行对应组的id

            var g_datas = all_gdata.Where(g => g.Group_id == g_id).ToList();
            if (g_datas.Count == 0) return;
            var g_data = g_datas[0];

            int orgi_teacher = g_data.T_id_first.HasValue ? (int)g_data.T_id_first : -1; //保存选中组原有的指导老师，这里指定另一个老师，原老师需要减去该组

            int tea_idx = Teachers_view.CurrentCell.RowIndex;
            var teacher_id = all_teacher[tea_idx].teacher_id;//现在选中的老师id

            var teacher_group = all_tdata.Where(a_t => a_t.Teacher_id == teacher_id).ToList();//对应老师在t_data中的记录

            if (teacher_group.Count == 0) //如果这个老师还没选组，在t_data中没有记录，则新建一条记录
            {
                Models.TeaChoice t1 = new TeaChoice();
                t1.Teacher_id = teacher_id;
                t1.Assign_1 = g_id;//把组分配给这新老师
                t1.res_count = 1;
                all_tdata.Add(t1); //在t_data中添加一条新纪录
                g_data.T_id_first = teacher_id; //在g_data表中，修改这组的指导老师
                modi_orgTeacher(orgi_teacher, g_id);//删除原来这组的指导老师
            }
            else if (teacher_group[0].res_count < LogInfo.Sys_Config.tearch_choice)//如果这个老师选了组，还没有达到上限
            {
                for (int i = 1; i <= 5; i++)
                {
                    var g = get_Choice(teacher_group[0], $"Assign_{i}");
                    if (g == -1)
                    {
                        set_Choice(teacher_group[0], $"Assign_{i}", g_id);
                        teacher_group[0].res_count++;
                        break;
                    }
                }

                g_data.T_id_first = teacher_id; //在g_data表中，修改这组的指导老师
                modi_orgTeacher(orgi_teacher, g_id);//删除原来这组的指导老师
            }
            else
            {
                MessageBox.Show("教师指导队伍，已经超过总数");
                return;
            }

            Disp_teachers_group();
        }

        void modi_orgTeacher(int tid, int gid)
        {
            if (tid == -1) 
                return;

            var teacher = all_tdata.Where(a_t => a_t.Teacher_id == tid).FirstOrDefault();

            for (int i = 1; i <= 5; i++)
            {
                var g = get_Choice(teacher, $"Assign_{i}");
                if (g == gid)
                {
                    set_Choice(teacher, $"Assign_{i}", -1);
                    teacher.res_count--;
                    break;
                }
            }
        }

        private void teach_Group_MouseClick_1(object sender, MouseEventArgs e)
        {
            if (teach_Group.CurrentCell == null) 
                return;

            int idx = teach_Group.CurrentCell.RowIndex;
            Disp_Group_info(idx, t_groups);
        }

        private void Disp_Group_info(int idx, List<StuGroup> stu_Groups)
        {
            Group_info.Text = stu_Groups[idx].project_info;
            Group_name.Text = stu_Groups[idx].group_name;
            Project_name.Text = stu_Groups[idx].project_name;

            var g_member = all_students.Where<Student>(stu => stu.group_id == stu_Groups[idx].group_id).ToList();
            Group_member.DataSource = g_member;
        }

        private void save_Click(object sender, EventArgs e)//保存按钮
        {
            BLL.B_Admin b_T = new BLL.B_Admin();
            BLL.B_Admin b_G = new BLL.B_Admin();
            try
            {               
                b_T.Save_result_T(all_tdata);               
                b_G.Save_result_S(all_gdata);
                MessageBox.Show("保存成功");
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Groups_view_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Group_member_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void teach_Group_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Teachers_view_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        private void Group_member_MouseClick(object sender, MouseEventArgs e)
        {

        }       

    }
}
